<--- %%NOBANNER%% --> BUGtoSAS.sas
 BackForward
/*-------------------<---Start of Description-->---------------------\
| Convert the output of BUGS to SAS;                                 |
| The output created by BUGS is found in two files that are written  |
| to a directory of the users' specification. These two files are by |
| default named bugs.ind and bugs.out. These two files can be renamed|
| if desired. The bugs.ind file contains index information describing|
| the bugs.out file. The BUGtoSAS macro uses the information in these|
| two files to create a SAS dataset containing the posterior samples.|
| All variable names will be truncated to the first 8 characters. By |
| convention, periods are used in denoting variable names in the BUGS|
| programming language. Since SAS does not allow periods in the      |
| variable names, all periods will be converted to underscores.      |
|------------------------------------------------|
|--------------------------------------------------------------------|
|-----------<---Start of Files or Arguements Needed-->---------------|
| Arguments:                                                         |
|    newdata - specify the name of the new SAS dataset to created;   |
|    direc   - specify the directory holding the BUGS output files;  |
|    ind     - specify the name of the BUGS index file;              |
|    out     - specify the name of the BUGS output file;             |
|    stats   - the stats option prints summary statistics for all    |
|              posterior samples; options include stats = summary    |
|              statistics, nostats = no summary statistics;          |
|------------<---End of Files or Arguements Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| Example: %BUGtoSAS(newdata, d:\temp\bugs\examples\, bugs.ind,      |
|                    bugs.out, stats);                               |
| Usage: %BUGtoSAS(newdata,direc,ind,out,stats) ;                    |
\------------------<---Start of Files Created-->--------------------*/
%macro BUGtoSAS(newdata,direc,ind,out,stats) ;
/*--------------------------------------------\
| Author:  Matthew Hayat                      |
|          Division of Biostatistics          |
|          Medical College of Wisconsin       |
| Purpose: Convert output data set from BUGS  |
|          to SAS;                            |
\--------------------------------------------*/
%put ;
%put Written by Matthew Hayat, Division of Biostatistics, Medical College of Wisconsin ;
%put Last Updated 11/2/2000 ;
%put ;

%let ind = "%cmpres(&direc&ind)" ;
%let out = "%cmpres(&direc&out)" ;

data info ind (keep=ratio) ;
   infile &ind end=lstrec delimiter='09'x ;
   input varname $ first last ;
   output info ;
   ratio = last / _n_ ;
   if lstrec then output ind ;
   call symput("numrow",_n_) ;

data out (keep=value) ;
   infile &out delimiter='09'x ;
   input sampnum value ;

proc transpose data=info out=&newdata ;
   var first ;
   id varname ;

data &newdata (drop=_name_) ;
   set &newdata ;
   if _n_ < 1 ;

data all
   %if &stats = stats %then %do ;
   temp
   %end ; ;
   if _n_ < 1 ;

%do i = 1 %to &numrow ;
   data v&i ;
      if _n_ = 1 then set ind ;
      set out ;
      if (ratio*%eval(&i)-ratio+1) le _n_ le (ratio*%eval(&i)) ;
      v&i = value ;
   data all ;
      merge all v&i ;
   %if &stats = stats %then %do ;

      proc univariate noprint data=v&i ;
         var v&i ;
         output out=v&i n=n mean=mean var=var std=std min=min max=max
                        range=range pctlpts=2.5 median=median pctlpts=97.5
                        kurtosis=kurtosis skewness=skewness pctlpre=p   ;

      data temp ;
         set temp v&i ;
    %end ;
%end ;

data &newdata ;
   set all (keep=v1-v%cmpres(&numrow)) &newdata ;

data &newdata (drop=i v1-v%cmpres(&numrow)) ;
   set &newdata ;
   array numer(*) _numeric_ ;
   array vvec(*) v1-v%cmpres(&numrow) ;
   do i = 1 to dim(vvec) ;
      numer(%eval(&numrow)+i)= vvec(i) ;
   end ;

%if &stats = stats %then %do ;
   data temp ;
      merge info (keep=varname) temp ;

      proc print label noobs uniform data=temp ;
      title "Summary Statistics" ;
      var n mean var std min max range p2_5 median p97_5 kurtosis skewness ;
      id varname ;
      label varname  = 'Variable'
         n        = 'N'
         mean     = 'Mean'
         var      = 'Variance'
         std      = 'Standard Deviation'
         min      = 'Minimum'
         max      = 'Maximum'
         range    = 'Range'
         p2_5     = '2.5%'
         median   = '50%'
         p97_5    = '97.5%'
         kurtosis = 'Kurtosis'
         skewness = 'Skewness' ;
%end ;
proc datasets ;
   delete v1-v%cmpres(&numrow) all out ind info temp ;
   title " " ;
run;quit; %let syslast=&newdata;
%mend ;